package com.bsh.serviceImpl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.bsh.api.ApiUtils;
import com.bsh.api.params.DtoMerchantInfo;
import com.bsh.api.params.DtoShop;
import com.bsh.dto.MerchantListQryDto;
import com.bsh.mybatis.dataView.MerchantImages;
import com.bsh.mybatis.model.Merchant;
import com.bsh.mybatis.model.User;
import com.bsh.repository.DaoMerchant;
import com.bsh.repository.DaoUser;
import com.bsh.service.MerchantService;
import com.bsh.utils.Pager;

/**
 * 商户
 * 
 * @author Administrator
 *
 */
@Service
@Transactional
public class MerchantServiceImpl implements MerchantService {
	@Autowired
	private DaoMerchant merchantDao;
	@Autowired
	private DaoUser userDao;

	/* (non-Javadoc)
	 * @see com.bsh.serviceImpl.MerchantService#getPage(com.bsh.utils.Pager, com.bsh.dto.MerchantListQryDto)
	 */
	@Override
	public List<Map<String, Object>> getPage(Pager pager, MerchantListQryDto qryParams) {
		StringBuilder sb = new StringBuilder();
		if (!StringUtils.isEmpty(qryParams.getUserName()))
			sb.append(String.format(" and u.name like '%%%s%%' ", qryParams.getUserName().trim()));
		if (!StringUtils.isEmpty(qryParams.getMobile()))
			sb.append(String.format(" and m.mobile like '%%%s%%' ", qryParams.getMobile().trim()));
		if (!StringUtils.isEmpty(qryParams.getMerchantName()))
			sb.append(String.format(" and m.merchant_name like '%%%s%%' ", qryParams.getMerchantName().trim()));

		if (!StringUtils.isEmpty(qryParams.getCreateTime()) && StringUtils.isEmpty(qryParams.getCreateTime1())) {
			sb.append(String.format(" and m.create_time >='%s' ", qryParams.getCreateTime().trim()));
		} else if (StringUtils.isEmpty(qryParams.getCreateTime()) && !StringUtils.isEmpty(qryParams.getCreateTime1())) {
			sb.append(String.format(" and m.create_time <='%s 23:59:59' ", qryParams.getCreateTime1().trim()));
		} else if (!StringUtils.isEmpty(qryParams.getCreateTime())
				&& !StringUtils.isEmpty(qryParams.getCreateTime1())) {
			sb.append(String.format(" and (m.create_time between '%s' and '%s 23:59:59') ", qryParams.getCreateTime().trim(),
					qryParams.getCreateTime1().trim()));
		}

		List<Map<String, Object>> list = merchantDao.getPage(pager, sb.toString());
		return list;
	}
	
	@Override
	public List<Map<String, Object>> getPageExport(MerchantListQryDto qryParams) {
		StringBuilder sb = new StringBuilder();
		if (!StringUtils.isEmpty(qryParams.getUserName()))
			sb.append(String.format(" and u.name like '%%%s%%' ", qryParams.getUserName().trim()));
		if (!StringUtils.isEmpty(qryParams.getMobile()))
			sb.append(String.format(" and m.mobile like '%%%s%%' ", qryParams.getMobile().trim()));
		if (!StringUtils.isEmpty(qryParams.getMerchantName()))
			sb.append(String.format(" and m.merchant_name like '%%%s%%' ", qryParams.getMerchantName().trim()));

		if (!StringUtils.isEmpty(qryParams.getCreateTime()) && StringUtils.isEmpty(qryParams.getCreateTime1())) {
			sb.append(String.format(" and m.create_time >='%s' ", qryParams.getCreateTime().trim()));
		} else if (StringUtils.isEmpty(qryParams.getCreateTime()) && !StringUtils.isEmpty(qryParams.getCreateTime1())) {
			sb.append(String.format(" and m.create_time <='%s 23:59:59' ", qryParams.getCreateTime1().trim()));
		} else if (!StringUtils.isEmpty(qryParams.getCreateTime())
				&& !StringUtils.isEmpty(qryParams.getCreateTime1())) {
			sb.append(String.format(" and (m.create_time between '%s' and '%s 23:59:59') ", qryParams.getCreateTime().trim(),
					qryParams.getCreateTime1().trim()));
		}

		List<Map<String, Object>> list = merchantDao.getPageExport(sb.toString());
		return list;
	}
	

	/* (non-Javadoc)
	 * @see com.bsh.serviceImpl.MerchantService#getAskPage(com.bsh.utils.Pager, java.lang.String, java.lang.String, java.lang.Integer)
	 */
	@Override
	public List<Map<String, Object>> getAskPage(Pager pager, String userName, String mobile, Integer status) {
		StringBuilder sb = new StringBuilder();
		if (!StringUtils.isEmpty(userName))
			sb.append(String.format(" and u.name like '%%%s%%'", userName.trim()));
		if (!StringUtils.isEmpty(mobile))
			sb.append(String.format(" and m.mobile like '%%%s%%'", mobile.trim()));
		if (status != null && status != -1)
			sb.append(String.format(" and m.status = %s ", status));
		List<Map<String, Object>> list = merchantDao.getAskPage(pager, sb.toString());
		return list;
	}

	/* (non-Javadoc)
	 * @see com.bsh.serviceImpl.MerchantService#addMerchant(com.bsh.mybatis.model.Merchant)
	 */
	@Override
	public Integer addMerchant(Merchant merchantReq) {
		merchantDao.addMerchant(merchantReq);
		return merchantReq.getId();
	}

	/* (non-Javadoc)
	 * @see com.bsh.serviceImpl.MerchantService#audit(int, boolean, java.lang.String)
	 */
	@Override
	public int audit(int merchantId, boolean isPass, String auditMsg) {
		Merchant mer = merchantDao.selectByPrimaryKey(merchantId);
		if (isPass){
			int res=merchantDao.audit(merchantId, true, "");			
			userDao.setAsMerchant(mer.getUserid());
			return res;
		}
		else{
			int res=merchantDao.audit(merchantId, false, auditMsg);
			userDao.updateIsMerchant(mer.getUserid());
			return res;
		}
	}

	/* (non-Javadoc)
	 * @see com.bsh.serviceImpl.MerchantService#getMerchant(int)
	 */
	@Override
	public Merchant getMerchant(int mid) {
		Merchant mt = merchantDao.selectByPrimaryKey(mid);
		return mt;
	}

	/* (non-Javadoc)
	 * @see com.bsh.serviceImpl.MerchantService#editMerchantExludeImages(com.bsh.mybatis.model.Merchant)
	 */
	@Override
	public int editMerchantExludeImages(Merchant merchant) {
		int mt = merchantDao.editMerchantExludeImages(merchant);
		
		
		MerchantImages images = merchantDao.getMerchantImages(merchant.getId());
		if(images==null)images=new MerchantImages();
		if (!StringUtils.isEmpty(merchant.getCertificate()))
			images.setCertificate(merchant.getCertificate());
		if (!StringUtils.isEmpty(merchant.getIdcardA()))
			images.setIdcardA(merchant.getIdcardA());
		if (!StringUtils.isEmpty(merchant.getIdcardB()))
			images.setIdcardB(merchant.getIdcardB());
		if (!StringUtils.isEmpty(merchant.getImgCover()))
			images.setImgCover(merchant.getImgCover());
		if (!StringUtils.isEmpty(merchant.getImgCoverSmall()))
			images.setImgCoverSmall(merchant.getImgCoverSmall());
		images.setId(merchant.getId());

		merchantDao.editMerchantImages(images);
		return mt;
	}

	/* (non-Javadoc)
	 * @see com.bsh.serviceImpl.MerchantService#addMerchant(java.lang.String, com.bsh.mybatis.model.Merchant)
	 */
	@Override
	public int addMerchant(String userName, Merchant merchant) {
		User user = userDao.selectByName(userName);
		if (user == null)
			return -100;
        
		Merchant mer = merchantDao.getByName(merchant.getName());
		if(mer!=null)
        	return-101;
		
		mer = merchantDao.getMerchantByMerchantName(merchant.getMerchantName());
		if(mer!=null)
        	return-102;
		
		merchant.setUserid(user.getId());
		merchant.setStatus(0);
		merchant.setCreateTime(new Date());
		int mt = merchantDao.insert(merchant);
		return mt;
	}

	@Override
	public List<Map<String, Object>> apiGetShopList(Pager pager, DtoShop qryParams) {
		StringBuilder sb = new StringBuilder();
		
		if (ApiUtils.objIsNotEmpty(qryParams.getKeyword()))
			sb.append(String.format(" and m.merchant_name like '%%%s%%' ", qryParams.getKeyword().trim()));
		if (qryParams.getCategory() != null && qryParams.getCategory() >0 )
			sb.append(String.format(" and m.category = %d ", qryParams.getCategory()));
		if (qryParams.getCity() != null && qryParams.getCity() >0 )
			sb.append(String.format(" and m.city = %d ", qryParams.getCity()));
		
		List<Map<String, Object>> list = merchantDao.apiGetShopList(pager, sb.toString());
		return list;
	}

	@Override
	public Integer apiAddMerchant(Merchant merchantReq) {
		merchantDao.apiAddMerchant(merchantReq);
		return merchantReq.getId();
	}
	
	/**
	 * 根据名称查询商家
	 */
	@Override
	public Merchant getMerchantByName(String name){
		Merchant res = merchantDao.getMerchantByName(name);
		return res;
	}
	
	/**
	 * 根据商家全称查询商家
	 */
	@Override
	public Merchant getMerchantByMerchantName(String MerchantName){
		Merchant res = merchantDao.getMerchantByMerchantName(MerchantName);
		return res;
	}
	
	/**
	 * 根据userId 查属于某用户的商家list
	 * @return
	 */
	@Override
	public List<Map<String, Object>> getMerchantByUserId(Integer userId){
		
		List<Map<String, Object>> list = merchantDao.getMerchantByUserId(userId);
		return list;
	}
    
	/**
	 * 根据id查询商户详情
	 */
	@Override
	public List<DtoMerchantInfo> getMerchantDetailById(Integer id) {
		List<DtoMerchantInfo> list=merchantDao.getMerchantDetailById(id);
		return list;
	}

	@Override
	public Integer changeMerchantInfo(Merchant merchant) {
		return merchantDao.updateByPrimaryKeySelective(merchant);
	}

	@Override
	public List<Map<String, Object>> apiGetHotShopList() {
		return merchantDao.apiGetHotShopList();
	}
	
	@Override
	public Integer setRecomendSort(Integer id, Integer sort) {
		return merchantDao.setRecomendSort(id,sort);
	}
}
